home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / getpwent.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  249 lines

  1. RCS_ID_C="$Id: getpwent.c,v 2.2 1994/02/17 02:19:40 ppessi Exp $";
  2. /*
  3.  * User database functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP usergroup.library
  8.  *
  9.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Sun Nov 28 17:45:55 1993 ppessi
  13.  * Last modified: Thu Jan 27 12:14:02 1994 ppessi
  14.  *
  15.  * $Log: getpwent.c,v $
  16.  * Revision 2.2  1994/02/17  02:19:40  ppessi
  17.  * Saving before downgrading library
  18.  *
  19.  * Revision 2.1  1994/01/23  03:18:14  ppessi
  20.  * Updated the netinfo.device commands
  21.  *
  22.  * Revision 1.4  1994/01/21  08:13:47  ppessi
  23.  * Updated documentation
  24.  *
  25.  * Revision 1.3  1994/01/19  10:04:06  ppessi
  26.  * Added error checking
  27.  *
  28.  * Revision 1.2  94/01/15  05:40:43  ppessi
  29.  * Hard disk crash
  30.  *
  31.  * Revision 1.1  93/11/30  03:31:28  ppessi
  32.  * Initial revision
  33.  */
  34.  
  35. /****** usergroup.library/getpwent *******************************************
  36.  
  37.    NAME
  38.         getpwent, getpwnam, getpwuid, setpwent, endpwent
  39.          - password database operations
  40.  
  41.    SYNOPSIS
  42.         #include <pwd.h>
  43.  
  44.         pw = getpwuid(uid)
  45.         D0            D0
  46.         struct passwd *getpwuid(uid_t);
  47.  
  48.  
  49.         pw = getpwnam(name)
  50.         D0             A1
  51.         struct passwd *getpwnam(const char *);
  52.  
  53.         pw = getpwent()
  54.         D0
  55.         struct passwd *getpwent(void);
  56.  
  57.         setpwent()
  58.         void setpwent(void);
  59.  
  60.         endpwent()
  61.         void endpwent(void);
  62.  
  63.    FUNCTION
  64.         These functions operate on the user database via netinfo.device
  65.         interface. They provide convenient unix-compatible interface to the
  66.         password unit of the netinfo.device.
  67.  
  68.         The local password database is stored in the file AmiTCP:db/passwd,
  69.         its format is described in netinfo.device/passwd.  The entry
  70.         returned by each reading function is defined by the structure passwd
  71.         found in the include file <pwd.h>:
  72.  
  73.                struct passwd
  74.                {
  75.                  char  *pw_name;         \* Username *\
  76.                  char  *pw_passwd;       \* Encrypted password *\
  77.                  pid_t  pw_uid;          \* User ID *\
  78.                  gid_t  pw_gid;          \* Group ID *\
  79.                  char  *pw_gecos;        \* Real name etc *\
  80.                  char  *pw_dir;          \* Home directory *\
  81.                  char  *pw_shell;        \* Shell *\
  82.                };
  83.  
  84.         The functions getpwnam() and getpwuid() search the password database
  85.         for the given login name or user uid, respectively, always returning
  86.         the first one encountered.
  87.  
  88.         The getpwent() function sequentially reads the password database and
  89.         is intended for programs that wish to process the complete list of
  90.         users.
  91.  
  92.         All three routines will open the password unit of netinfo.device for
  93.         reading, if necesssary.
  94.  
  95.         The setpwent() function opens the password unit of netinfo.device.
  96.         The endpwent() function closes the password unit of netinfo.device.
  97.         It is recommended to call endpwent() if the program won't access
  98.         password database any more.
  99.  
  100.    RESULTS
  101.         The functions getpwent(), getpwnam() and getpwuid() return a valid
  102.         pointer to a passwd structure on success and a null pointer if end
  103.         of database is reached or an error occurs. The functions endpwent()
  104.         and setpwent() have no return value.
  105.  
  106.     ERRORS
  107.         [ENOENT] -- the netinfo.device could not be opened.
  108.  
  109.         Other netinfo.device IO errors can be retrieved by ug_GetErr().
  110.  
  111.    FILES
  112.         AmiTCP:db/passwd    The password database file
  113.  
  114.    SEE ALSO
  115.         getgrent(), netinfo.device/passwd
  116.  
  117.    HISTORY
  118.         The functions getpwent(), getpwnam(), getpwuid(), setpwent() and
  119.         endpwent() functions appeared in Version 7 AT&T UNIX.
  120.  
  121.    BUGS
  122.         These functions leave their results in an internal static object and
  123.         return a pointer to that object. Subsequent calls to these function
  124.         will modify the same object. If you need re-entrant operation, you
  125.         should use directly the netinfo.device.
  126.  
  127.    COMPATIBILITY
  128.         The BSD passwd database handling routines setpwfile() and
  129.         setpassent() are fairly useless in a networked environment and they
  130.         are not implemented.
  131.  
  132. *****************************************************************************
  133. *
  134. */
  135.  
  136. #include "base.h"
  137. #include "libfunc.h"
  138.  
  139. static short done_set_ent = 0;
  140.  
  141. SAVEDS ASM void R_endpwent(void)
  142. {
  143.   ObtainSemaphore(ni_lock);
  144.   done_set_ent = 0;
  145.   CloseNIUnit(NETINFO_PASSWD_UNIT);
  146.   ReleaseSemaphore(ni_lock);
  147. }
  148.  
  149. SAVEDS ASM void R_setpwent(void)
  150. {
  151.   struct NetInfoReq *nreq;
  152.  
  153.   ObtainSemaphore(ni_lock);
  154.  
  155.   if (nreq = OpenNIUnit(NETINFO_PASSWD_UNIT)) {
  156.     nreq->io_Command = CMD_RESET;
  157.     myDoIO(nreq);
  158.     done_set_ent = 1;
  159.   } else {
  160.     SetDeviceErr();
  161.   }
  162.  
  163.   ReleaseSemaphore(ni_lock);
  164. }
  165.  
  166. SAVEDS ASM struct passwd *R_getpwent(void)
  167. {
  168.   struct NetInfoReq *nreq;
  169.   struct passwd *pw = NULL;
  170.  
  171.   ObtainSemaphore(ni_lock);
  172.   if (nreq = OpenNIUnit(NETINFO_PASSWD_UNIT)) {
  173.  
  174.      /* do setpwent() if necessary */
  175.     if (!done_set_ent) {
  176.       nreq->io_Command = CMD_RESET;
  177.       myDoIO(nreq);
  178.       done_set_ent = 1;
  179.     }
  180.  
  181.     nreq->io_Command = CMD_READ;
  182.     if (myDoIO(nreq) == 0) {
  183.       pw = (struct passwd *)nreq->io_Data;
  184.     } else {
  185.       SetDeviceErr();
  186.     }
  187.   } else {
  188.     SetDeviceErr();
  189.   }
  190.  
  191.   ReleaseSemaphore(ni_lock);
  192.  
  193.   return pw;
  194. }
  195.  
  196. SAVEDS ASM struct passwd *R_getpwuid(REG(d0) uid_t uid)
  197. {
  198.   struct NetInfoReq *nreq;
  199.   struct passwd *pw = NULL;
  200.  
  201.   ObtainSemaphore(ni_lock);
  202.   if (nreq = OpenNIUnit(NETINFO_PASSWD_UNIT)) {
  203.     pw = (struct passwd *)nreq->io_Data;
  204.     pw->pw_uid = uid;
  205.     nreq->io_Command = NI_GETBYID;
  206.  
  207.     if (myDoIO(nreq) != 0) {
  208.       pw = NULL;
  209.       SetDeviceErr();
  210.     }
  211.   } else {
  212.     SetDeviceErr();
  213.   }
  214.  
  215.   ReleaseSemaphore(ni_lock);
  216.  
  217.   return pw;
  218. }
  219.  
  220. SAVEDS ASM struct passwd *R_getpwnam(REG(a1) const char *name)
  221. {
  222.   struct NetInfoReq *nreq;
  223.   struct passwd *pw = NULL;
  224.  
  225.   if (name == NULL) {
  226.     SetErrno(EFAULT);
  227.     return NULL;
  228.   }
  229.  
  230.   ObtainSemaphore(ni_lock);
  231.   if (nreq = OpenNIUnit(NETINFO_PASSWD_UNIT)) {
  232.  
  233.     pw = (struct passwd *)nreq->io_Data;
  234.     pw->pw_name = (char *)name;
  235.     nreq->io_Command = NI_GETBYNAME;
  236.  
  237.     if (myDoIO(nreq) != 0) {
  238.       pw = NULL;
  239.       SetDeviceErr();
  240.     }
  241.   } else {
  242.     SetDeviceErr();
  243.   }
  244.  
  245.   ReleaseSemaphore(ni_lock);
  246.  
  247.   return pw;
  248. }
  249.